package com.renwu.edu.info.manager.controller;

import com.renwu.edu.info.manager.domain.Student;
import com.renwu.edu.info.manager.domain.Teacher;
import com.renwu.edu.info.manager.service.TeacherService;

import java.util.Scanner;


public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();

    public void start() {
        lo:
        while (true) {
            System.out.println("--------------------欢迎来到教师系统--------------------");
            System.out.println("1.添加老师\t\t2.删除老师\t\t3.修改老师\t\t4.查看老师\t\t5.退出");
            System.out.println("请输入你的选择");
            int chose = sc.nextInt();
            switch (chose) {
                case 1:
                    //System.out.println("添加");
                    AddTeacher();
                    break;
                case 2:
                    //System.out.println("删除");
                    deleteTeacher();
                    break;
                case 3:
                    //System.out.println("修改");
                    setTeacher();
                    break;
                case 4:
                    //System.out.println("查看");
                    findAllTeacher();
                    break;
                case 5:
                    System.out.println("退出教师管理系统");
                    break lo;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }
        }
    }

    public void setTeacher() {
        int id = inputTeacherId();
        Teacher teacher = inputTeacherInfo(id);
        teacherService.setTeacher(id,teacher);
        System.out.println("修改成功");
    }

    public void deleteTeacher() {
        int id = inputTeacherId();
        teacherService.deleteTeacher(id);
        System.out.println("删除成功");
    }

    public void findAllTeacher() {
        //通过teacherService中的find方法获取数组，数组为需要打印的对象，
        Teacher[] teachers = teacherService.findAllTeacher();
        //对获取的数组进行判断，若数组中无元素，则提示添加
        if (teachers == null) {
            System.out.println("无教师信息，请先添加");
            //当数组为空时，执行至此，后续代码可以不用实现，因为该查看信息为空，所以可以直接结束，使用return返回即可；
            return;
        }
        //if语句不成立，说明teacher数组不为空
        // 对数组进行遍历，取出元素，并打印在控制台。
        for (int i = 0; i < teachers.length; i++) {
            //避免空指针异常
            //假如数组中第一个元素不为null，或者[a,null，null,null,null]这种情况时，则只打印不为空的信息，剩余为空的位置不理会
            if (teachers[i] != null) {
                System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
                //获取元素并且打印在控制台；
                System.out.println(teachers[i].getId() + "\t\t" + teachers[i].getName() + "\t\t" + teachers[i].getAge() + "\t\t" + teachers[i].getBirthday());
            }
        }

    }

    public void AddTeacher() {
        int id;
        while (true) {
            System.out.println("请输入添加的教师学号");
            id = sc.nextInt();
            boolean result = teacherService.isExists(id);
            if (result) {
                System.out.println("该教师学号已经存在，请重新输入");
            } else {
                break;
            }
        }
        Teacher tea = inputTeacherInfo(id);
        boolean result = teacherService.AddTeacher(tea);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    public int inputTeacherId(){
        int id;
        while (true){
            System.out.println("请输入教师信息学号");
            id = sc.nextInt();
            boolean exists = teacherService.isExists(id);
            if(exists){
                break;
            }else{
                System.out.println("该教师信息不存在,请重新输入");
            }
        }
        return id;
    }

    public Teacher inputTeacherInfo(int id){
        System.out.println("请输入教师姓名");
        String name = sc.next();
        System.out.println("请输入教师年龄");
        int age = sc.nextInt();
        System.out.println("请输入教师生日");
        String birthday = sc.next();
        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
    }
}
